package com.team11.server.service.student;

import com.team11.server.config.SplicingConfig;
import com.team11.server.mapper.SInformationMapper;
import com.team11.server.mapper.StudentHomeworkMapper;
import com.team11.server.pojo.CFiles;
import com.team11.server.pojo.student.StudentHomeworkTask;
import com.team11.server.util.file.GetFilesUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author DuKang
 * @Date  2021/7/12 8:44
 * @Desc:
 * @Version: 1.0
 */
@Service
public class StudentHomeworkServiceImpl {
    @Resource
    private StudentHomeworkMapper studentHomeworkMapper;
    @Resource
    private SInformationMapper sInformationMapper;

    /**
     * 获取学生作业表
     * @param sNumber 学生学号
     * @param status 作业状态
     * @param hSetime 发布时间
     * @param kcName 项目名称
     * @param hTitle 作业主题
     * @param hType 作业类型
     * @param tName 实训老师
     * @param hStoptime 结束时间
     */
    public Object getHomeworkList(String sNumber, Integer status,String hSetime, String kcName,
                                   String hTitle, Integer hType, String tName, String hStoptime){
        Integer sid=sInformationMapper.getSidById(sNumber);
        if (sid==null){
            return "该学生不存在！";
        }
        return studentHomeworkMapper.getHomework(sid, status,hSetime, kcName, hTitle, hType, tName, hStoptime);
    }

    /**
     * 根据作业id获取作业任务
     * @param hId 作业id
     */
    public StudentHomeworkTask getHomeworkTask(Integer hId){
        return  studentHomeworkMapper.getHomeworkTask(hId);
    }

    /**
     * 获取老师批复
     * @param hId 作业id
     * @param sNumber 学生学号
     */
    public Object getTeacherReply(Integer hId,String sNumber){
        Integer s=sInformationMapper.getSidById(sNumber);
        if (s==null){
            return "该学生不存在！";
        }
        return studentHomeworkMapper.getTeacherReply(hId,s);
    }

    /**
     * 更新学生作业
     * @param sNumber 学生学号
     * @param hId 作业id
     * @param answer 学生答案
     */
    public Object updateAnswer(String sNumber, Integer hId, String answer,
                               MultipartFile[] files, HttpServletRequest request){

        Integer s=sInformationMapper.getSidById(sNumber);
        if (s==null){
            return "该学生不存在！";
        }
        //附件
        CFiles cFiles = new CFiles();
        List<Object> list1 = new ArrayList<>();
        List<Object> list2 = new ArrayList<>();
        //附件
        if (files != null) {
            List list3 = GetFilesUtil.getFiles(files, request);
            list1.add(list3.get(0));
            list2.add(list3.get(1));
            cFiles.setCFName(SplicingConfig.Splicing(list1));
            cFiles.setCFile(SplicingConfig.Splicing(list2));
            //保存到数据库代码，存入路径以及文件名称
            cFiles.setSid(s);
            cFiles.setHId(hId);
            cFiles.setHomeworkId(studentHomeworkMapper.getHomeworkId(s,hId));
        }
        if("".equals(studentHomeworkMapper.getCFId(s,hId))) {
            Integer row1 = studentHomeworkMapper.insertHomeworkFiles(cFiles);
        }else {
            Integer row1=studentHomeworkMapper.updateHomeworkFiles(cFiles);
        }
        Date date=new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Integer row=studentHomeworkMapper.updateHomeworkAnswer(answer, sf.format(date),studentHomeworkMapper.getCFId(s,hId),s,hId);
        if (row>0){
            return "作业提交成功";
        } else{
            return "作业提交失败";
        }
    }

    /**
     * 获取学生答案
     * @param sNumber 学生学号
     * @param hId 作业id
     */
    public Object getAnswer(String sNumber,Integer hId){
        Integer s=sInformationMapper.getSidById(sNumber);
        if (s==null){
            return "该学生不存在！";
        }
        return studentHomeworkMapper.getHomeworkAnswer(s,hId);
    }
}
